"""
课程模型
"""
from sqlalchemy import Column, String, Integer, Numeric, Text, Enum, ForeignKey, DateTime, func
from sqlalchemy.orm import relationship
from .base import Base


class Course(Base):
    """课程表"""
    __tablename__ = "courses"
    
    name = Column(String(100), nullable=False, comment="课程名称")
    subject_id = Column(Integer, ForeignKey("subjects.id"), nullable=False, comment="科目ID")
    total_stages = Column(Integer, default=4, comment="总阶段数")
    total_price = Column(Numeric(10, 2), nullable=False, comment="课程总价")
    description = Column(Text, comment="课程描述")
    status = Column(Enum("active", "inactive", name="course_status"), 
                   default="active", comment="状态")
    
    # 关系
    subject = relationship("Subject", back_populates="courses")
    stages = relationship("CourseStage", back_populates="course")
    orders = relationship("Order", back_populates="course")
    student_assignments = relationship("StudentCourseAssignment", back_populates="course")
    teacher_assignments = relationship("TeacherStageAssignment", back_populates="course")
    student_progress = relationship("StudentProgress", back_populates="course")
    course_schedules = relationship("CourseSchedule", back_populates="course")
    
    def __repr__(self):
        return f"<Course(id={self.id}, name='{self.name}', total_price={self.total_price})>"


class CourseStage(Base):
    """课程阶段表"""
    __tablename__ = "course_stages"
    
    course_id = Column(Integer, ForeignKey("courses.id"), nullable=False, comment="课程ID")
    stage_number = Column(Integer, nullable=False, comment="阶段序号")
    stage_name = Column(String(100), nullable=False, comment="阶段名称")
    stage_price = Column(Numeric(10, 2), nullable=False, comment="阶段价格")
    description = Column(Text, comment="阶段描述")
    
    # 关系
    course = relationship("Course", back_populates="stages")
    
    def __repr__(self):
        return f"<CourseStage(id={self.id}, course_id={self.course_id}, stage_number={self.stage_number})>"
